﻿''' <summary>
''' 发布 订阅的中间类
''' 使用方法，在发布中定义类对象，并引发事件。
''' 在订阅类中注册回调函数。
''' </summary>
Public Class ClsBroker

    '=======================================
    '用于窗体间传递参数值
    '---------------------------------------
    '定义一个委托
    Public Delegate Sub TransmateObjectEvent(ByVal sender As Object, ByVal e As TransEventArg)

    '事件的委托列表，用来注册回调函数
    Public Shared onChangeList As List(Of TransmateObjectEvent)

    '用来同步的对象
    Public Shared m_obj As Object

    '定义一个自定义事件
    Public Shared Custom Event OnChange As TransmateObjectEvent
        '添加回调函数
        AddHandler(value As TransmateObjectEvent)
            If onChangeList Is Nothing Then
                onChangeList = New List(Of TransmateObjectEvent)

            End If
            If m_obj Is Nothing Then
                m_obj = New Object()
            End If

            '同步锁
            SyncLock m_obj
                onChangeList.Add(value)
            End SyncLock

        End AddHandler

        '移除回调函数
        RemoveHandler(value As TransmateObjectEvent)
            If onChangeList Is Nothing Then
                Return
            End If

            SyncLock m_obj
                onChangeList.Remove(value)
            End SyncLock
        End RemoveHandler

        '引发事件，广播回调
        RaiseEvent(sender As Object, e As TransEventArg)
            If onChangeList Is Nothing Then
                Return
            End If

            For Each tmpH As TransmateObjectEvent In onChangeList
                tmpH.Invoke(sender, e)
            Next
        End RaiseEvent
    End Event

    Public Sub BroadCastEvent(ByVal e As TransEventArg)
        '引发事件
        RaiseEvent OnChange(Me, e)
    End Sub
    '=====================================================

    '=====================================================
    '定义一个自定义事件,用来从自定义控件中发布内容更改通知
    '-----------------------------------------------------
    Public Delegate Sub CustomControlContentChangeEvent(ByVal sender As Object, ByVal e As CustomControlChangeNotifyEventArg)

    Public Shared OnContentChanges As List(Of CustomControlContentChangeEvent)
    Public Shared ObjContent As Object '同步锁

    Public Shared Custom Event OnCustomCtrolContentChange As CustomControlContentChangeEvent
        AddHandler(value As CustomControlContentChangeEvent)
            If OnContentChanges Is Nothing Then
                OnContentChanges = New List(Of CustomControlContentChangeEvent)
            End If
            If ObjContent Is Nothing Then
                ObjContent = New Object()
            End If
            SyncLock ObjContent
                OnContentChanges.Add(value)
            End SyncLock
        End AddHandler

        RemoveHandler(value As CustomControlContentChangeEvent)
            If OnContentChanges Is Nothing OrElse OnContentChanges.Count = 0 Then
                Return
            End If

            SyncLock ObjContent
                OnContentChanges.Remove(value)
            End SyncLock
        End RemoveHandler

        RaiseEvent(sender As Object, e As CustomControlChangeNotifyEventArg)
            If OnContentChanges Is Nothing OrElse OnContentChanges.Count = 0 Then
                Return
            End If

            '广播事件
            For Each tmpH As CustomControlContentChangeEvent In OnContentChanges
                tmpH.Invoke(sender, e)
            Next
        End RaiseEvent
    End Event

    ''' <summary>
    ''' 定义一个广播事件,用于自定义控件调用
    ''' </summary>
    ''' <param name="e"></param>
    Public Sub BroadcastCustomControlContentChange(ByVal e As CustomControlChangeNotifyEventArg)
        RaiseEvent OnCustomCtrolContentChange(Me, e)
    End Sub

    '=====================================================
End Class

''' <summary>
''' 窗体体间传递值的调用参数
''' </summary>
Public Class TransEventArg
    Inherits EventArgs

    '声明一个信息，用来标识各途中数据
    Private m_Msg As String
    '除信息外的对象数据用此传递
    Private m_Obj As Object

    Public Property Msg As String
        Get
            Return m_Msg
        End Get
        Set(value As String)
            m_Msg = value
        End Set
    End Property

    Public Property Obj As Object
        Get
            Return m_Obj
        End Get
        Set(value As Object)
            m_Obj = value
        End Set
    End Property

End Class

''' <summary>
''' 自定义控件内容修改的传递参数
''' </summary>
Public Class CustomControlChangeNotifyEventArg
    Inherits EventArgs

    '标记内容是否修改过
    Private m_ContentChanged As Boolean
    Private m_OldContent As Object
    Private m_NewContent As Object
    Private m_FormWehere As String
    '标记内容是哪种类型,
    Private m_ContentType As Type

    Public Property ContentChanged As Boolean
        Get
            Return m_ContentChanged
        End Get
        Set(value As Boolean)
            m_ContentChanged = value
        End Set
    End Property

    Public Property OldContent As Object
        Get
            Return m_OldContent
        End Get
        Set(value As Object)
            m_OldContent = value
        End Set
    End Property

    Public Property NewContent As Object
        Get
            Return m_NewContent
        End Get
        Set(value As Object)
            m_NewContent = value
        End Set
    End Property

    Public Property ContentType As Type
        Get
            Return m_ContentType
        End Get
        Set(value As Type)
            m_ContentType = value
        End Set
    End Property

    Public Property FormWehere As String
        Get
            Return m_FormWehere
        End Get
        Set(value As String)
            m_FormWehere = value
        End Set
    End Property
End Class
